package com.wandian.texas.db;

import java.sql.SQLException;

import com.wandian.texas.dao.ChatrecordDao;
import com.wandian.texas.dao.ServerListDao;
import com.wandian.texas.dao.TestDao;
import com.wandian.texas.dao.UserInfoDao;
import com.wandian.texas.dao.Impl.ChatrecordDaoImpl;
import com.wandian.texas.dao.Impl.FriendsDaoImpl;
import com.wandian.texas.dao.Impl.GameRoomDaoImpl;
import com.wandian.texas.dao.Impl.LobbyDaoImpl;
import com.wandian.texas.dao.Impl.ServerListDaoImpl;
import com.wandian.texas.dao.Impl.TestDaoImpl;
import com.wandian.texas.dao.Impl.UserInfoDaoImpl;

/**
 * 除了线程池之外的线程使用，如,main未初始化线程池，需要数据库操作
 * 
 * @author long
 * @Create 2012-10-19 下午3:24:16
 * @QQ 108232706
 * @version 1.0
 * @description CommonDaoManager
 */
public class CommonDaoManager {
	private static DBMgr[] dbMgrs;
	private static UserInfoDao[] userInfoDao;
	private static ChatrecordDao[] chatDao;
	private static TestDao[] testDao;
	private static ServerListDaoImpl[] serverListDao;
	private static FriendsDaoImpl[] friendsDao;
	private static LobbyDaoImpl[] lobbyDao;
	private static GameRoomDaoImpl[] gameRoomDao;
	private static int threadCount;
	private static boolean isInit = false;
	public static synchronized boolean init(int _threadCount) throws Throwable {
		if (isInit)
			return true;
		isInit = true;
		threadCount = _threadCount;
		int count = 0;
		dbMgrs = new DBMgr[threadCount];
		userInfoDao = new UserInfoDaoImpl[threadCount];
		chatDao = new ChatrecordDaoImpl[threadCount];
		testDao = new TestDaoImpl[threadCount];
		serverListDao = new ServerListDaoImpl[threadCount];
		friendsDao = new FriendsDaoImpl[threadCount];
		lobbyDao = new LobbyDaoImpl[threadCount];
		gameRoomDao = new GameRoomDaoImpl[threadCount];
		do {
			dbMgrs[count] = new DBMgr(count);
			userInfoDao[count] = new UserInfoDaoImpl(dbMgrs[count]);
			chatDao[count] = new ChatrecordDaoImpl(dbMgrs[count]);
			testDao[count] = new TestDaoImpl(dbMgrs[count]);
			friendsDao[count] = new FriendsDaoImpl(dbMgrs[count]);
			serverListDao[count] = new ServerListDaoImpl(dbMgrs[count]);
			lobbyDao[count] = new LobbyDaoImpl(dbMgrs[count]);
			gameRoomDao[count] = new GameRoomDaoImpl(dbMgrs[count]);
			count++;
		} while (count < threadCount);
		return true;
	}

	public synchronized static boolean close() throws SQLException {

		for (int i = 0; i < threadCount; i++) {
			dbMgrs[i].close();
		}
		return true;

	}

	public synchronized static UserInfoDao UserInfoDao(int index) {
		if (index < 0 || index >= threadCount)
			throw new IllegalArgumentException("index invalid:" + index);
		return userInfoDao[index];
	}

	public synchronized static ChatrecordDao ChatrecordDao(int index) {
		if (index < 0 || index >= threadCount)
			throw new IllegalArgumentException("index invalid:" + index);
		return chatDao[index];
	}

	public synchronized static TestDao TestDao(int index) {
		if (index < 0 || index >= threadCount)
			throw new IllegalArgumentException("index invalid:" + index);
		return testDao[index];
	}

	public synchronized static ServerListDao ServerListDao(int index) {
		if (index < 0 || index >= threadCount)
			throw new IllegalArgumentException("index invalid:" + index);
		return serverListDao[index];
	}

	public synchronized static FriendsDaoImpl FriendsDao(int index) {
		if (index < 0 || index >= threadCount)
			throw new IllegalArgumentException("index invalid:" + index);
		return friendsDao[index];
	}

	public synchronized static LobbyDaoImpl LobbyDao(int index) {
		if (index < 0 || index >= threadCount)
			throw new IllegalArgumentException("index invalid:" + index);
		return lobbyDao[index];
	}

	public synchronized static GameRoomDaoImpl GameRoomDao(int index) {
		if (index < 0 || index >= threadCount)
			throw new IllegalArgumentException("index invalid:" + index);
		return gameRoomDao[index];
	}
}
